$(function () {
    var markid = getUrlString('markid');
    var type = getUrlString('type');
    // var markid = 'b88afc62e44094ff46749eb4b55cfadc';
    var page = -1,totalSize = 0,sort= 0;
    cashCommodityList(0,sort);
    function cashCommodityList (page,sort) {
        layLoading();
        var data = {
            'markid':markid,
            'mainstate':1,
            'type':type,
            'sort':sort,
            'page':page
        };
        getData('post','/niu/apiv3/niu_getuserkuv3_full.php',data,function (data) {
            console.log(data);
            layer.closeAll('loading');
            if(data.resultcode == 0 ){
                var append = '';

                $(data.data).each(function () {
                    if(this.lockon === '0'){
                        append += '<li>' +
                            '            <input class="kuid" type="hidden" value="'+this.id+'">'+
                            '            <input type="hidden" class="isLockon" value="'+this.lockon+'"> '+
                            '            <div class="pic">' +
                            '                <img src="'+this.pics+'-toysbox11'+'" alt="">' +
                            '                <img class="noCheck" src="../../res/img/my/myEggPool/noCheck.png" alt="">' +
                            '                <img class="check" src="../../res/img/my/myEggPool/check.png" alt="">' +
                            '                        <div class="dayNum">'+this.outtime+'</div>' +
                            '            </div>' +
                            '            <div class="name">'+this.name+'</div>' +
                            '        </li>'
                    }

                });
                $('#cashCommodityList').append(append);
                $('#loaded').show();
                //点击选择商品
                $('#cashCommodityList li').click(function () {
                    if($(this).hasClass('selected')){
                        $(this).removeClass('selected');
                        $(this).find('.pic').children('.check').hide();
                        $(this).children('.kuid').removeClass('forId');
                        var checkNum = $('.forId').length;
                        $('#checkNum').html('('+checkNum+')')
                    }else {
                        $(this).addClass('selected');
                        $(this).find('.pic').children('.check').show();
                        $(this).children('.kuid').addClass('forId');
                        var checkNum = $('.forId').length;
                        $('#checkNum').html('('+checkNum+')')
                    }
                });

                //全选
                $('.checkAllSelected').click(function () {
                    $(this).hide();
                    $('.cashCommodityList li').addClass('selected');
                    $('.kuid').addClass('forId');
                    $('.check,.noCheckAll').show();
                    var checkNum = $('.forId').length;
                    $('#checkNum').html('('+checkNum+')')
                });

                //取消全选
                $('.noCheckAll').click(function () {
                    $('.check,.noCheckAll').hide();
                    $('.cashCommodityList li').removeClass('selected');
                    $('.kuid').removeClass('forId');
                    $('.checkAllSelected').show();
                    var checkNum = $('.forId').length;
                    $('#checkNum').html('('+checkNum+')')
                });
                // mRefresh.endUpLoading(true);
            }else {
                laymsg(data.msg)
            }
        })
    }


    $('.pickUpBtn').click(function () {
        var pickUpArr = [];
        $('.forId').each(function () {
            pickUpArr.push($(this).val())
        });
        var pickUpStr = pickUpArr.join(';');
        if (pickUpStr == ''){
            laymsg('请选择锁库商品')
        } else {
            checktoys(pickUpStr);
        }

    });

    $('.cancel').click(function () {
        nativeCommand("dancool://activity=fin")
    });

    function checktoys(kutoysids) {
        console.log(kutoysids);
        var data = {
            'markid':markid,
            'set':'lock',
            'kutoysids':kutoysids
        };
        getData('post','/niu/apiv3/niu_dolockon.php',data,function (data) {
            console.log(data);
            if(data.resultcode == 0 ){
                laymsg('锁库成功');
                setTimeout(function () {
                    nativeCommand("dancool://activity=reflashfin")
                },500)
            }else {
                laymsg(data.msg)
            }
        })
    }


});